package io.finer.erp.jeecg.purchase.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequest;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequestLine;
import io.finer.erp.jeecg.purchase.service.PurchaseRequestLineService;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestLinePage;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 采购申请行 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-12
 */
@Api(tags = "采购申请行")
@RestController
@RequestMapping("/purchase/purchaseRequestLine")
public class PurchaseRequestLineController {

    @Autowired
    PurchaseRequestLineService purchaseRequestLineService;

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "明细通过主表ID查询")
    @ApiOperation(value="明细主表ID查询", notes="明细-通主表ID查询")
    @GetMapping(value = "/queryLineByMainId")
    public Result<?> queryLineListByMainId(@RequestParam(name="id",required=true) String id) {
        QueryWrapper<PurchaseRequestLine> queryWrapper = new QueryWrapper<>();
        List<PurchaseRequestLine> purchaseRequestLineList = purchaseRequestLineService.list(queryWrapper.eq("mid", id));
        return Result.ok(purchaseRequestLineList);
    }

    @AutoLog(value="采购申请行-待处理采购申请-分页列表查询")
    @ApiOperation(value="采购申请行-待处理采购申请-分页列表查询", notes = "采购申请行-待处理采购申请-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(PurchaseRequestLinePage purchaseRequestLinePage,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<PurchaseRequestLinePage> queryWrapper = QueryGenerator.initQueryWrapper(purchaseRequestLinePage, req.getParameterMap());
        Page<PurchaseRequest> page = new Page<>(pageNo, pageSize);
        IPage<PurchaseRequestPage> pageList = purchaseRequestLineService.selectPurchaseRequestLinePage(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "采购申请行-编辑")
    @ApiOperation(value="采购申请行-编辑", notes="采购申请行-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PurchaseRequestLine purchaseRequestLine) {
        PurchaseRequestLine purchaseRequestLineEntity = purchaseRequestLineService.getById(purchaseRequestLine.getId());
        if(purchaseRequestLineEntity==null) {
            return Result.error("未找到对应数据");
        }
        purchaseRequestLineService.updateById(purchaseRequestLine);
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "采购申请行-创建采购订单")
    @ApiOperation(value="采购申请行-创建采购订单", notes="采购申请行-创建采购订单")
    @PutMapping(value = "/createPurchaseOrder")
    public Result<?> createPurchaseOrder(@RequestBody JSONObject json) {
        List<String> purchaseOrderIds = purchaseRequestLineService.createPurchaseOrder(json.getJSONArray("ids").toJavaList(String.class));
        JSONObject result = new JSONObject();
        result.put("purchaseOrderIds", purchaseOrderIds);
        return Result.ok(result);
    }
}

